package com.mjh.zhujianju.service.party.impl;

import com.google.common.collect.Lists;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.mjh.zhujianju.dao.party.BuildingClassUsersMapper;
import com.mjh.zhujianju.model.party.BuildingClassDetailEO;
import com.mjh.zhujianju.model.party.BuildingClassUsersDetailsEO;
import com.mjh.zhujianju.model.party.BuildingClassUsersEO;
import com.mjh.zhujianju.service.party.BuildingClassDetailService;
import com.mjh.zhujianju.service.party.BuildingClassUsersDeatilsService;
import com.mjh.zhujianju.service.party.BuildingClassUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author liuyang
 * @Description:
 * @date 2018/9/5 21:43
 */
@Service
public class BuildingClassUsersServiceImpl extends BaseServiceImpl<BuildingClassUsersEO> implements BuildingClassUsersService {

    @Autowired
    private BuildingClassUsersMapper buildingClassUsersMapper;

    @Autowired
    private BuildingClassDetailService buildingClassDetailService;

    @Autowired
    private BuildingClassUsersDeatilsService buildingClassUsersDeatilsService;

    public BuildingClassUsersServiceImpl(BuildingClassUsersMapper buildingClassUsersMapper) {
        super(buildingClassUsersMapper);
    }

    @Override
    public void addJoinClass(Long userId, Long classId) {
        BuildingClassUsersEO eo = new BuildingClassUsersEO();
        eo.setClassId(classId);
        eo.setCreateUserId(userId);
        eo.setSchedule(0);
        eo.setCreateTime(new Date());
        buildingClassUsersMapper.insertSelective(eo);

        List<BuildingClassDetailEO> buildingClassDetailEOList = buildingClassDetailService.selectBuildingClassDetailByClassId(classId);

        BuildingClassUsersDetailsEO buildingClassUsersDetailsEO;
        List<BuildingClassUsersDetailsEO> list = Lists.newArrayList();

        for (BuildingClassDetailEO buildingClassDetailEO : buildingClassDetailEOList){
            buildingClassUsersDetailsEO = new BuildingClassUsersDetailsEO();
            buildingClassUsersDetailsEO.setClassUsersId(eo.getId());
            buildingClassUsersDetailsEO.setClassDetailId(buildingClassDetailEO.getId());
            buildingClassUsersDetailsEO.setCreateTime(new Date());
            buildingClassUsersDetailsEO.setSchedule(0);
            list.add(buildingClassUsersDetailsEO);
        }
        buildingClassUsersDeatilsService.insertByBatch(list);
    }

    @Override
    public List<BuildingClassUsersEO> findBuildingClassUsersEOByUserId(Long userId) {
        BuildingClassUsersEO eo = new BuildingClassUsersEO();
        eo.setCreateUserId(userId);
        return buildingClassUsersMapper.selectListByCondition(eo);
    }

    @Override
    public void updateClassUser(Long userId, Long classId) {
        //查出记录
        BuildingClassUsersEO eo = new BuildingClassUsersEO();
        eo.setCreateUserId(userId);
        eo.setClassId(classId);
        List<BuildingClassUsersEO> list = buildingClassUsersMapper.selectListByCondition(eo);
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        //删除记录
        buildingClassUsersMapper.deleteByPrimaryKey(list.get(0).getId());
    }

    @Override
    public void updateClassUserRestore(Long userId, Long classId) {
        //查出记录
        BuildingClassUsersEO eo = new BuildingClassUsersEO();
        eo.setCreateUserId(userId);
        eo.setClassId(classId);
        List<BuildingClassUsersEO> list = buildingClassUsersMapper.selectListByCondition(eo);
        if (CollectionUtils.isEmpty(list)){
            return;
        }

        //进度改为0
        BuildingClassUsersEO buildingClassUsersEO = list.get(0);
        buildingClassUsersEO.setSchedule(0);
        buildingClassUsersMapper.updateByPrimaryKeySelective(buildingClassUsersEO);

        //详情进度改为0
        buildingClassUsersDeatilsService.updateBuildingClassUsersDetailsByClassUsersId(buildingClassUsersEO.getId());
    }

    @Override
    public List<BuildingClassUsersEO> findBuildingClassUsersEOByUserId(Long userId, Long classId) {
        BuildingClassUsersEO eo = new BuildingClassUsersEO();
        eo.setCreateUserId(userId);
        eo.setClassId(classId);
        return buildingClassUsersMapper.selectListByCondition(eo);
    }

    @Override
    public List<BuildingClassUsersEO> queryBuildingClassUsersEOByUserIdAndClassId(Long userId, Long classId) {
        return buildingClassUsersMapper.queryBuildingClassUsersEOByUserIdAndClassId( userId,  classId);
    }
}
